const CesiumPolyline = React.createClass({
	displayName:'CesiumPolyline',
    componentDidMount(){
        const polylines = this.props.polylines;

        //设置自定义label的默认不变属性
        if(polylines){
            this.polyline = polylines.add({
                positions:[],
                width:1.0,
                loop:false,//形成闭合环，可以用于measurement工具
                material:Cesium.Material.fromType('Color',{
                    color:Cesium.Color.RED
                })
            });
        }

        this.updatePolyline();
    },

    componentWillUnmount() {
        const polylines = this.props.polylines;

        if(polylines && !polylines.isDestroyed() && this.polyline) {
            polylines.remove(this.polyline);
        }
    },

    componentDidUpdate(prevProps) {
        if(!shallowEqual(prevProps, this.props)) {
            this.updatePolyline();
        }
    },

    updatePolyline() {
        const {coords, loop=false, color = "red" } = this.props;
        if(this.polyline) {
            const cartographics = coords.map((coord)=>Cesium.Cartographic.fromDegrees(coord.lon, coord.lat, coord.alt));
            const cartesians = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(cartographics);

            const colorValue = Cesium.Color.fromCssColorString(color);

            this.polyline.positions = cartesians;
            this.polyline.loop = !!loop;
            this.polyline.material.uniforms.color = colorValue;
        }
    },

    render() {
        return null;
    }
});